/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.campaign_targeting_service;

import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.hiddenequity.utils.City;
import com.hiddenequity.utils.Country;
import com.hiddenequity.utils.Province;
import com.hiddenequity.utils.XMLUtils;

public class CampaignTargetingResponseParser {

	Document _XMLDocument;


	/*
	 * Constructor
	 */
	public CampaignTargetingResponseParser(String XMLData){

		try {
			_XMLDocument = XMLUtils.XMLStringToDoc(XMLData);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}//Constructor
	
	
	
	/*
	 * Check response for errors
	 */
	public String checkForErrors(){
		String error = null;
		try {
	        
	        NodeList faultstring = _XMLDocument.getElementsByTagName("faultstring");
	        error =  XMLUtils.getCharacterDataFromElement((Element) faultstring.item(0));
	        
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
		return error;
	}//checkForErrors
	
	
	
	/*
	 * Get Campaign country target
	 */
	public ArrayList<Country> getCampaignGeoCountryTarget(){

		ArrayList<Country> selectedCountry = new ArrayList<Country>();
		
		try {
			NodeList nodes = _XMLDocument.getElementsByTagName("targets");
	        NodeList cNodes;
			for (int i = 0; i < nodes.getLength(); i++) {
	        	Element element = (Element) nodes.item(i);
	        	String target = element.getAttribute("xsi:type");
	        	
	        	if(target.equals("CountryTarget")){
	        		Country country = new Country();
	        		cNodes = element.getElementsByTagName("countryCode");
	        		country.setCode(XMLUtils.getCharacterDataFromElement((Element) cNodes.item(0)));
	        		selectedCountry.add(country);
	        	}
	        }
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
		return selectedCountry;	
	}//getCampaignGeoCountryTarget
	
	
	
	/*
	 * Get Campaign province target
	 */
	public ArrayList<Province> getCampaignGeoProvinceTarget(){

		ArrayList<Province> selectedProvince = new ArrayList<Province>();
		
		try {
			NodeList nodes = _XMLDocument.getElementsByTagName("targets");
	        NodeList cNodes;
			for (int i = 0; i < nodes.getLength(); i++) {
	        	Element element = (Element) nodes.item(i);
	        	String target = element.getAttribute("xsi:type");
	        	
	        	if(target.equals("ProvinceTarget")){
	        		Province province = new Province();
	        		cNodes = element.getElementsByTagName("provinceCode");
	        		province.setProvinceCode(XMLUtils.getCharacterDataFromElement((Element) cNodes.item(0)));
	        		selectedProvince.add(province);
	        	}
	        }
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
		return selectedProvince;			
	}
	
	
	
	/*
	 * Get Campaign city target
	 */
	public ArrayList<City> getCampaignGeoCityTarget(){
		ArrayList<City> selectedProvince = new ArrayList<City>();
		
		try {
			NodeList nodes = _XMLDocument.getElementsByTagName("targets");
	        NodeList cNodes;
			for (int i = 0; i < nodes.getLength(); i++) {
	        	Element element = (Element) nodes.item(i);
	        	String target = element.getAttribute("xsi:type");
	        	
	        	if(target.equals("CityTarget")){
	        		City city = new City();
	        		
	        		cNodes = element.getElementsByTagName("provinceCode");
	        		city.setProvinceCode(XMLUtils.getCharacterDataFromElement((Element) cNodes.item(0)));
	        		
	        		cNodes = element.getElementsByTagName("countryCode");
	        		city.setCountryCode(XMLUtils.getCharacterDataFromElement((Element) cNodes.item(0)));
	        		
	        		cNodes = element.getElementsByTagName("cityName");
	        		city.setCityName(XMLUtils.getCharacterDataFromElement((Element) cNodes.item(0)));
	        		
	        		selectedProvince.add(city);
	        	}
	        }
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
		return selectedProvince;		
	}
	
	
	
	/*
	 * Parse campaign data, to display in edit form
	 * Only one campaign
	 */
	public ArrayList<String> getCampaignLangTarget(){
		ArrayList<String> campaignData = new ArrayList<String>();
		
		try {
			NodeList nodes = _XMLDocument.getElementsByTagName("languageCode");
	        
			for (int i = 0; i < nodes.getLength(); i++) {
	        	Element element = (Element) nodes.item(i);
	        	campaignData.add(XMLUtils.getCharacterDataFromElement(element));
	        }
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }

		return campaignData;
	}//getCampaignLangTarget		
	
	
	
	/*
	 * Parse campaign data, to display in edit form
	 * Only one campaign
	 */
	public ArrayList<String> getCampaignPlatformTarget(){
		ArrayList<String> campaignData = new ArrayList<String>();
		
		try {
			NodeList nodes = _XMLDocument.getElementsByTagName("platformType");
	        
			for (int i = 0; i < nodes.getLength(); i++) {
	        	Element element = (Element) nodes.item(i);
	        	campaignData.add(XMLUtils.getCharacterDataFromElement(element));
	        }
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }

		return campaignData;
	}//getCampaignPlatformTarget		
	
	
	
	/*
	 * Parse campaign data, to display in edit form
	 * Only one campaign
	 */
	public ArrayList<String> getCampaignMobileTarget(){
		ArrayList<String> campaignData = new ArrayList<String>();
		
		try {
			NodeList nodes = _XMLDocument.getElementsByTagName("platformName");
	        
			for (int i = 0; i < nodes.getLength(); i++) {
	        	Element element = (Element) nodes.item(i);
	        	campaignData.add(XMLUtils.getCharacterDataFromElement(element));
	        }
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }

		return campaignData;
	}//getCampaignMobileTarget	
		
}
